<?php

namespace App\Repository;
use App\Models\Customer\Customer;
use App\Params\Backend\CustomerListParams;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

/**
 * 客户数据仓储类
 *
 * @package App\Http\Repository
 * @author WangBin
 * @mail: <849850352@qq.com>
 */
class CustomerRepository extends BaseRepository
{

    /**
     * 获取客户（过滤条件）
     *
     * @param CustomerListParams $filter
     * @return
     */
    public function getList(CustomerListParams $filter)
    {
        $customers = DB::table('b2c_customer as a')
            ->select(['a.*', 'b.grade_name', 'c.group_name'])
            ->leftJoin('b2c_customer_grade as b', 'a.grade_id', '=', 'b.grade_id')
            ->leftJoin('base_group as c', 'a.group_id', '=', 'c.group_id');
        if (!empty($filter->getNickname())) {
            $customers->where('a.nickname', 'like', '%' . $filter->getNickname() . '%');
        }
        if (!empty($filter->getMobile())) {
            $customers->where('a.mobile', 'like', '%' . $filter->getMobile() . '%');
        }
        if (!empty($filter->getGender())) {
            $customers->where('a.gender', $filter->getGender());
        }
        if (!empty($filter->getGradeId())) {
            $customers->where('a.grade_id', $filter->getGradeId());
        }
        if (!empty($filter->getGroupId())) {
            $customers->where('a.group_id', $filter->getGroupId());
        }
        $customers->orderByDesc('a.created_at');
        return $customers;
    }

    /**
     * 根据ID获取客户
     *
     * @param int $customerId
     * @param bool $lock
     * @return \Illuminate\Database\Eloquent\Builder|Model|object|null
     */
    public function getById(int $customerId, bool $lock = false)
    {
        $query = Customer::query()->where('customer_id', $customerId);
        if ($lock) {
            $query->lockForUpdate();
        }
        return $query->first();
    }

    /**
     * 根据手机号查询客户
     *
     * @param string $mobile
     * @return \Illuminate\Database\Eloquent\Builder|Model|object|null
     */
    public function getByMobile(string $mobile)
    {
        return Customer::query()->where('mobile', $mobile)->first();
    }

    /**
     * 保存客户信息
     *
     * @param array $data
     * @return int|mixed
     */
    public function saveData(array $data)
    {
        $query = empty($data['customer_id']) ? new Customer() : Customer::find($data['customer_id']);
        foreach ($data as $field => $value) {
            $query->{$field} = $value;
        }
        return $query->save() ? $query->customer_id : 0;
    }

    /**
     * 更新客户余额
     *
     * @param int $customerId
     * @param float $amount
     * @return int
     */
    public function updateBalance(int $customerId, float $amount)
    {
        $query = Customer::query()->where('customer_id', $customerId)
            ->where('balance', '>=', $amount);
        if ($amount < 0) {
            return $query->decrement('balance', $amount);
        } else {
            return $query->increment('balance', $amount);
        }
    }

    /**
     * 根据用户名获取客户
     *
     * @param string $username
     * @return \Illuminate\Database\Eloquent\Builder|Model|object|null
     */
    public function getByUsername(string $username)
    {
        return Customer::query()->where('username', $username)->where('deleted', 0)->first();
    }

    /**
     * 根据群组 Id 获取客户
     *
     * @param int $groupId
     * @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
     */
    public function getByGroupId(int $groupId)
    {
        return Customer::query()->where('group_id', $groupId)->where('is_active', 1)->get();
    }
}
